/**
 * 
 */
Ext.Loader.setConfig({
			enabled : true
		});

Ext.Loader.setPath('Ext.ux', './js/ext4/ux');

Ext.require(['Ext.data.*', 'Ext.grid.*', 'Ext.tree.*', 'Ext.ux.TreePicker']);

var url, flag = Ext.util.Cookies.get('flag');
Ext.util.Cookies.set('flag', null);
Ext.define('Message', {
			extend : 'Ext.data.Model',
			fields : [{
						name : 'id'
					}, {
						name : 'myid'
					}, {
						name : 'senderid'
					}, {
						name : 'sendername'
					}, {
						name : 'title'
					}, {
						name : 'content'
					}, {
						name : 'sendtime'
					}, {
						name : 'status' /* 信息状态 */
					}, {
						name : 'lasttime'
					}, {
						name : 'receoverid'
					}, {
						name : 'receovername'
					}, {
						name : 'readtime'
					}, {
						name : 'mystatus'/* 我的信息状态 1.未读，2.已读，3.删除 */
					}]
		});

var store = Ext.create('Ext.data.Store', {
			model : 'Message',
			pageSize : 20,
			proxy : {
				type : 'ajax',
				url : 'messageAction!queryPage.action',
				reader : {
					type : 'json',
					root : 'pageList'
				}
			}
		});
var satusStore = Ext.create('Ext.data.Store', {
			fields : ['id', 'name'],
			data : [{
						'id' : 0,
						'name' : '全部'
					}, {
						'id' : 1,
						'name' : '未读'
					}, {
						'id' : 2,
						'name' : '已读'
					}]
		});
var satusCombo = Ext.create('Ext.form.field.ComboBox', {
			store : satusStore,
			name : 'status',// 控件名称
			displayField : 'name',
			valueField : 'id',// 实际值
			emptyText : '搜索',
			queryMode : 'local',
			listeners : {
				select : function(combo, records, eOpts) { // 该处实现自动对store加载
					var proxy = store.getProxy(); // 获取store里面的proxy全部
					proxy.extraParams = {
						"status" : records[0].data.id
					};
					store.reload();
				}
			}
		});
function openDetail(id, mystatus, myid) {
	if (mystatus == 1) {
		Ext.Ajax.request({
					url : 'messageAction!read.action?messid=' + myid,
					success : function(response) {
						Ext.util.Cookies.set('messageId', id);
						window.top.Utils
								.addPage('messageQuery' + id, '查看信息',
										'js/resources/business/message/messageQuery.js');
					},
					failure : function(response, opts) {
						console.log('server-side failure with status code '
								+ response.status);
					}
				});

	} else {
		Ext.util.Cookies.set('messageId', id);
		window.top.Utils.addPage('messageQuery' + id, '查看信息',
				'js/resources/business/message/messageQuery.js');
	}
}
var grid = Ext.create('Ext.grid.Panel', {
	store : store,
	border : 0,
	selModel : Ext.create('Ext.selection.CheckboxModel'),
	columns : [{
				text : 'id',
				dataIndex : 'id',
				hidden : true
			}, {
				text : 'myid',
				dataIndex : 'myid',
				hidden : true
			}, {
				renderer : function(value, metadata, record, rowIndex,
						colIndex, store) {
					if (record.get('mystatus') == 1) {
						return "<img src='images/message_new.png'/>";
					} else {
						return "<img src='images/message_read.png'/>";
					}

				},
				width : 28
			}, {
				text : '标题',
				sortable : true,
				dataIndex : 'title',
				renderer : function(value, metadata, record, rowIndex,
						colIndex, store) {
					var id = record.get('id');
					var myid = record.get('myid');
					var mystatus = record.get('mystatus');
					var style_new = 'font-family:仿宋;color:#15428b;cursor: pointer;font-weight:bold;text-decoration: underline;';
					var style_read = 'font-family:仿宋;cursor: pointer;text-decoration: underline;';
					if (mystatus == 1) {
						return '<span style="' + style_new + '"  data="'
								+ value + '" onclick="openDetail(' + id + ','
								+ mystatus + ',' + myid + ')">' + value
								+ '</span>';
					} else {
						return '<span style="' + style_read + '"  data="'
								+ value + '" onclick="openDetail(' + id + ','
								+ mystatus + ',' + myid + ')">' + value
								+ '</span>';
					}

				},
				flex : 2
			}, {
				text : '发送者',
				sortable : true,
				dataIndex : 'sendername',
				flex : 2
			}, {
				text : '发送时间',
				sortable : true,
				renderer : function(value, metadata, record, rowIndex,
						colIndex, store) {
					if (value) {
						if (value) {
							var dates = value.substr(0, 10).split("-");
							var datetime = new Date(dates[0], dates[1] - 1,
									dates[2], value.substr(11, 2), value
											.substr(14, 2), value.substr(17, 2));
							return Ext.Date.format(datetime, 'Y年m月d日 H:i:s');
						}

					}

				},
				dataIndex : 'sendtime',
				flex : 2
			}, {
				text : '状态',
				sortable : true,
				dataIndex : 'mystatus',
				renderer : function(v) {
					if (v == 1) {
						return '未读';
					}
					if (v == 2) {
						return '已读';
					}
					if (v == 3) {
						return '删除';
					}
				},
				flex : 1
			}],
	viewConfig : {
		forceFit : true,
		getRowClass : function(record, rowIndex, rowParams, store) {
			if (record.data.mystatus == "1") {
				return 'my_row_Red';
			}
		}
	},
	stripeRows : true,
	tbar : [{
		xtype : 'button',
		text : '删除',
		iconCls : 'icon-delete_trash',
		handler : function() {
			var selMode = grid.getSelectionModel();
			var model = selMode.getSelection();
			if (model[0]) {
				var myids = [];// 删除多个
				Ext.Array.each(model, function(item) {
							myids.push(item.raw.myid);
						});
				Ext.Msg.show({
					title : '确认删除',
					buttons : Ext.MessageBox.OKCANCEL,
					msg : '确认删除？',
					fn : function(btn) {
						if (btn == 'ok') {
							Ext.Ajax.request({
								url : 'messageAction!deleteMy.action?myids='
										+ myids,
								success : function(response) {
									Ext.Msg.show({
												title : '提示',
												msg : '删除成功！',
												buttons : Ext.Msg.OK,
												width : 300,
												fn : function() {
													store.reload();
												}
											});
								},
								failure : function(response, opts) {
									console
											.log('server-side failure with status code '
													+ response.status);
								}
							});
						}
					}
				});

			} else {
				Ext.Msg.alert('提示', '请选择要删除的数据行！');
			}
		}
	}, '->', satusCombo],
	bbar : Ext.create('Ext.PagingToolbar', {
				pageSize : 20,// 每页20条
				store : store,
				displayInfo : true,
				plugins : Ext.create('Ext.ux.ProgressBarPager', {})
			})
});
function ExternalRefresh() {
	var proxy = store.getProxy();
	if (satusCombo.getValue() != null) {
		proxy.extraParams = {
			'status' : satusCombo.getValue()
		};
	}
	store.reload();
};
Ext.onReady(function() {
			Ext.create('Ext.container.Viewport', {
						layout : 'fit',
						items : [grid]
					});
			if (flag == 'demo') {// 主页跳转
				satusCombo.setValue(1);
			}
			// store.load();
			ExternalRefresh();
		});
